package com.bvgol.example.rabbitmq.listener;

import com.alibaba.fastjson.JSON;
import com.bvgol.example.rabbitmq.constant.Name;
import com.bvgol.example.rabbitmq.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * @author GUOCHEN
 */
@Service
public class OrderCloseListener {

    @SneakyThrows
    @RabbitListener(queues = "order.release.order.queue")
    public void listener(OrderEntity order, Channel channel, Message message){

        Date createTime = order.getCreateTime();
        LocalDateTime localDateTime = Instant.ofEpochMilli(createTime.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
        long between = ChronoUnit.SECONDS.between(localDateTime, LocalDateTime.now());
        String correlation = message.getMessageProperties().getHeader("spring_returned_message_correlation").toString();
        System.out.println("收到过期的订单信息,准备关闭订单:"+ correlation+":::::" + JSON.toJSONString(order)+"::::时间差"+between+":秒");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
